const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const Router = require('koa-router');
const cors = require('koa-cors');

const app = new Koa();
const router = new Router();

const models = require('./models')

models.sequelize.sync().then(() => {
  app.context.model = models;

  app.use(bodyParser());

  app.use(cors());

  router
    .get('/users', (ctx, next) => {
      // ...
    })
    .post('/users', (ctx, next) => {

      const body = ctx.request.body;

      const data = ctx.model.Users.create(body);

      ctx.body = {
        code: 0,
        msg: 'request success',
        data
      };

    })
    .put('/users/:id', (ctx, next) => {
      // ...
    })
    .del('/users/:id', (ctx, next) => {
      // ...
    })
    .all('/users/:id', (ctx, next) => {
      // ...
    });

  app.use(router.routes())
    .use(router.allowedMethods());

  app.listen(3100);
});